/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.wechatmsg.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserService;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinUserGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.service.CrudService;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinGroup;
import com.lczyfz.campusinfo.modules.wechatmsg.dao.WeixinGroupDao;

/**
 * 微信组别管理Service
 * @author 陈维乾
 * @version 2018-05-06
 */
@Service
@Transactional(readOnly = true)
public class WeixinGroupService extends CrudService<WeixinGroupDao, WeixinGroup> {

	@Autowired
	private WeixinGroupDao weixinGroupDao;

	@Autowired
	private WeixinUserGroupService weixinUserGroupService;

	@Autowired
	private CampusinfoUserService campusinfoUserService;

	public WeixinGroup get(String id) {
		return super.get(id);
	}
	
	public List<WeixinGroup> findList(WeixinGroup weixinGroup) {
		return super.findList(weixinGroup);
	}
	
	public Page<WeixinGroup> findPage(Page<WeixinGroup> page, WeixinGroup weixinGroup) {
		return super.findPage(page, weixinGroup);
	}
	
	@Transactional(readOnly = false)
	public void save(WeixinGroup weixinGroup) {
		super.save(weixinGroup);
	}
	
	@Transactional(readOnly = false)
	public void delete(WeixinGroup weixinGroup) {
		super.delete(weixinGroup);
	}

	public List<WeixinGroup> getAllList(){
		return weixinGroupDao.getAllList();
	}

	public HashMap<String,List<User>> getGroupUserMaps(){

		List<String> stringList = weixinGroupDao.getGroupNameAllList();

		HashMap<String,List<User>> stringListHashMap = new HashMap<>();

		for(String groupName:stringList){
			List<User> users = new ArrayList<>();
			List<String> weixinUserGroups = weixinUserGroupService.getListByGroupName(groupName);
			for(String openId: weixinUserGroups){
				User user = campusinfoUserService.getByOpenId(openId);
				users.add(user);
			}
			stringListHashMap.put(groupName,users);
		}
		
		return stringListHashMap;
	}

}